package com.ohos.carbon.slice.demos;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.model.DemoDataBean;
import com.ohos.carbon.provider.DemosAdapter;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.ElementScatter;

import java.util.ArrayList;
import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;

public class SearchToolbarSlice extends AbilitySlice {

    private Image back_button, image1, image2, searchfor_back_button, searchfor_image1;
    private Text text_title;
    private TextField searchfor_title_text;
    private ListContainer listContainer;
    private ArrayList<DemoDataBean> mList;
    private Component titlebar;
    private Component searchfor_layout, searchfor_layout_;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_search_toolbar);
        initData();
        initView();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Search Toolbar")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Search Toolbar")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Search Toolbar", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Search Toolbar", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Search Toolbar", 1);
            pf.flushSync();
        }
    }

    private void initData() {
        mList = new ArrayList<>();
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_man0, null, "Kathryn Wright", null, null));
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_man1, null, "Jacqueline Carol Hill", null, null));
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_man2, null, "Annie Williams", null, null));
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_man3, null, "Joyce Virginia Ross", null, null));
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_man4, null, "Sarah Theresa Lewis", null, null));
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_man5, null, "Joan Cook", null, null));
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_man6, null, "Christina Foster", null, null));
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_man7, null, "Harold Sanders", null, null));
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_man8, null, "Billy Davis", null, null));
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_man9, null, "Andrea Clark", null, null));
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_woman0, null, "Benjamin Thomas Carter", null, null));
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_woman1, null, "Steven Louis Watson", null, null));
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_woman2, null, "Patricia Doris Cook", null, null));
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_woman3, null, "Anthony Chris Martinez", null, null));
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_woman4, null, "Lori Morris", null, null));
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_woman5, null, "Ruth Hernandez", null, null));
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_woman6, null, "Ann Christina Barnes", null, null));
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_woman7, null, "Sandra Mildred Ross", null, null));
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_woman8, null, "Chris Cooper", null, null));
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_woman9, null, "Matin Johnny Griffin", null, null));
    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        searchfor_image1 = (Image) findComponentById(ResourceTable.Id_searchfor_image1);
        searchfor_back_button = (Image) findComponentById(ResourceTable.Id_searchfor_back_button);
        image1 = (Image) findComponentById(ResourceTable.Id_image1);
        image2 = (Image) findComponentById(ResourceTable.Id_image2);
        searchfor_title_text = (TextField) findComponentById(ResourceTable.Id_searchfor_title_text);
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_listview);
        image1.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_search));
        image2.setVisibility(Component.HIDE);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("Search");
        searchfor_layout = findComponentById(ResourceTable.Id_searchfor_layout);
        searchfor_layout_ = findComponentById(ResourceTable.Id_searchfor_layout_);

        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        searchfor_back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        back_button.setClickedListener(component -> onBackPressed());
        searchfor_back_button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                searchfor_layout.setVisibility(Component.VISIBLE);
                searchfor_layout_.setVisibility(Component.HIDE);
                searchfor_title_text.clearFocus();
            }
        });

        image1.setClickedListener(component -> {
            searchfor_layout.setVisibility(Component.HIDE);
            searchfor_layout_.setVisibility(Component.VISIBLE);
            searchfor_title_text.clearFocus();
        });

        searchfor_image1.setClickedListener(component -> {
            searchfor_title_text.setText("");
        });

        listContainer.setItemProvider(new DemosAdapter(mList, getContext()));
    }

    @Override
    protected void onBackground() {
        super.onBackground();
        searchfor_layout.setVisibility(Component.VISIBLE);
        searchfor_layout_.setVisibility(Component.HIDE);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
